<?php namespace App\Http\Services;

use Requests;
use Requests_Exception;

class DoRequest
{
    public static function doGet($url,array $header,$timeout = 10){
        $response = null;
        try {
            $response = Requests::get($url,$header,['timeout'=>$timeout]);
        } catch (Requests_Exception $ex) {
            return false;
        }

        if (!$response->success) {
            return false;
        }

        $result = null;
        if(is_object($response->body)){
            $result = json_decode($response->body);
        }else{
            $result = $response->body;
        }

        return $result;
    }

    public static function doPost($url, array $param,$timeout = 10)
    {
        $response = null;
        try {
//            $header = [
//                "Content-Type"=>"application/json"
//            ];
            $response = Requests::post($url,[], $param,['timeout'=>$timeout]);
        } catch (Requests_Exception $ex) {
            return false;
        }

        if (!$response->success) {
            $result = $response->body;
        }

        $result = null;
        if(is_object($response->body)){
            $result = json_decode($response->body);
        }else{
            $result = $response->body;
        }

        return $result;
    }
}